<?php

if( !$PARAPARA || !$PARAPARA->is_admin() )
    return;

function echo_info( $info_text ) {
    echo '<div class="padm_info">'; 
    echo $info_text;
    echo "</div>\n";
}

function echo_settrow( $label, $rowhtml ) {
    echo "<tr>\n";
    echo '<td class="padm_sett_edit_label">' . $label . "</td>\n";
    echo '<td class="padm_sett_edit_val">' . $rowhtml . "</td>\n";
    echo "</tr>\n";
}


function echo_descrow( $text) {
    echo "<tr>\n";
    echo '<td class="padm_sett_edit_desc" colspan="2">' . $text . "</td>\n";
    echo "</tr>\n";
}


function format_radio( $name, $values, $selected ) {

    $ret = '';
    foreach($values as $label => $val ) {

        if($val == $selected)
            $select_attr = 'checked="checked"';
        else
            $select_attr = '';

        $ret .= 
            '<span>' . "\n" .
            '<input name="' .$name .
            '" type="radio" class="radio" value="'.
            htmlspecialchars( $val ) . '" ' .
            $select_attr .' />' . $label .
            '</span>' . "\n";
    }

    return $ret;
}

function format_numeric( $name, $size, $value) {
    return
        '<input name="' . $name .
        '" class="text" size="' . $size . 
        '" value="' . htmlspecialchars( $value ) .'" />' . "\n";
}

function echo_select( $name, $values, $selected ) {
}


function echo_settingsform() {
    global $PARAPARA;

    $yes_no_bool = array('Yes' => 'true',
                         'No'  => 'false'
        );


    echo '<form action="?sec=sett&do=save" method="post">' , "\n";
    echo '<input type="hidden" value="1" name="formdata" />' , "\n";
    echo '<table class="padm_sett_edit">', "\n";

    // global settings ////////////////////////////////////////
    $global_enabled_radio   = format_radio('sett_glob_enabled',
                                           $yes_no_bool,
                                           $PARAPARA->config_get('GLOBAL', 'ON')
        );

    echo_descrow( 'Global settings' );
    echo_settrow( 'Enabled', $global_enabled_radio );

    // chat settings ////////////////////////////////////////
    $chat_enabled_radio   = format_radio('sett_chat_enabled',
                                         $yes_no_bool,
                                         $PARAPARA->config_get('CHAT', 'ON')
        );

    $chat_anonymous_radio = format_radio('sett_chat_allow_anonymous',
                                         $yes_no_bool,
                                         $PARAPARA->config_get('CHAT', 'ALLOW_ANONYMOUS')
        );

    $chat_shout_count = format_numeric('sett_chat_shout_count', 
                                       3, 
                                       $PARAPARA->config_get('CHAT', 'MAX_SHOUT_COUNT') 
        );

    echo_descrow( 'Chat settings');
    echo_settrow( 'Enabled', $chat_enabled_radio );
    echo_settrow( 'max_shout_count', $chat_shout_count );
    echo_settrow( 'allow_anonymous', $chat_anonymous_radio );

    // forum settings ////////////////////////////////////////
    $forum_enabled_radio   = format_radio('sett_forum_enabled',
                                         $yes_no_bool,
                                         $PARAPARA->config_get('FORUM', 'ON')
        );

    $forum_anonymous_radio = format_radio('sett_forum_allow_anonymous',
                                         $yes_no_bool,
                                         $PARAPARA->config_get('FORUM', 'ALLOW_ANONYMOUS')
        );

    $forum_thread_count = format_numeric('sett_forum_thread_count', 
                                       3, 
                                       $PARAPARA->config_get('FORUM', 'ACTIVE_THREAD_COUNT') 
        );

    echo_descrow( 'Forum settings');
    echo_settrow( 'Enabled', $forum_enabled_radio );
    echo_settrow( 'active_threadcount', $forum_thread_count );
    echo_settrow( 'allow_anonymous', $forum_anonymous_radio );

    echo "<tr>\n";
    echo '<td colspan="2" class="padm_sett_edit_buttons">' . "\n";
    echo '<input class="button" type="submit" name="settsave" value="Save"/>' ."\n";
    echo '<input class="button" type="submit" name="settres" value="Reset"/>' ."\n";
    echo "</td>\n</tr>\n";


    echo "</table>\n";
    echo "</form>\n";
}

function bool_check( $bb ) {
    if( ($bb == 'true') || ( $bb == 'false' ) )
        return true;
    return(false);
}

function num_check( $nn ) {
    if( is_numeric( $nn ) && ($nn >= 0) )
        return true;
    return false;
}

$dbl = $PARAPARA->dblayer;

echo "<h1>Settings</h2>\n";
if($_POST['settsave'] ) {
    $saved_something = false;

    $cur = $_POST['sett_glob_enabled'];
    if( bool_check( $cur ) && ( $cur != $PARAPARA->config_get('GLOBAL', 'ON')) ) {
        $PARAPARA->config_set_save( 'GLOBAL', 'ON', $cur );
        $saved_something = true;
    }

    $cur = $_POST['sett_chat_enabled'];
    if( bool_check( $cur ) && ( $cur != $PARAPARA->config_get('CHAT', 'ON')) ) {
        $PARAPARA->config_set_save( 'CHAT', 'ON', $cur );
        $saved_something = true;
    }

    $cur = $_POST['sett_chat_allow_anonymous'];
    if( num_check( $cur ) && ( $cur != $PARAPARA->config_get('CHAT', 'ALLOW_ANONYMOUS')) ) {
        $PARAPARA->config_set_save( 'CHAT', 'ALLOW_ANONYMOUS', $cur );
        $saved_something = true;
    }

    $cur = $_POST['sett_chat_shout_count'];
    if( num_check( $cur ) && ( $cur != $PARAPARA->config_get('CHAT', 'MAX_SHOUT_COUNT')) ) {
        $PARAPARA->config_set_save( 'CHAT', 'MAX_SHOUT_COUNT', $cur );
        $saved_something = true;
    }

    $cur = $_POST['sett_forum_enabled'];
    if( bool_check( $cur ) && ( $cur != $PARAPARA->config_get('FORUM', 'ON')) ) {
        $PARAPARA->config_set_save( 'FORUM', 'ON', $cur );
        $saved_something = true;
    }

    $cur = $_POST['sett_forum_allow_anonymous'];
    if( bool_check( $cur ) && ( $cur != $PARAPARA->config_get('FORUM', 'ALLOW_ANONYMOUS')) ) {
        $PARAPARA->config_set_save( 'FORUM', 'ALLOW_ANONYMOUS', $cur );
        $saved_something = true;
    }

    $cur = $_POST['sett_forum_thread_count'];
    if( num_check( $cur ) && ( $cur != $PARAPARA->config_get('FORUM', 'ACTIVE_THREAD_COUNT')) ) {
        $PARAPARA->config_set_save( 'FORUM', 'ACTIVE_THREAD_COUNT', $cur );
        $saved_something = true;
    }



    if($saved_something)
        echo_info("The changes have been saved");
    else
        echo_info("Nothing to save");

}
 
echo_settingsform();

?>